package com.jason.seckill.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jason.seckill.exception.GlobalException;
import com.jason.seckill.mapper.UserMapper;
import com.jason.seckill.pojo.User;
import com.jason.seckill.service.IUserService;
import com.jason.seckill.utils.CookieUtil;
import com.jason.seckill.utils.MD5Util;
import com.jason.seckill.utils.RedisUtil;
import com.jason.seckill.utils.UUIDUtil;
import com.jason.seckill.vo.LoginVo;
import com.jason.seckill.vo.RespBean;
import com.jason.seckill.vo.RespBeanEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author jason
 * @since 2021-10-22
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisUtil redisUtil;

    //@Override
    //public RespBean doLogin(LoginVo loginVo, HttpServletRequest request, HttpServletResponse response) {
    //    //获取信息
    //    String mobile = loginVo.getMobile();
    //    String password = loginVo.getPassword();
    //    //判断
    //    //if (mobile == null || password == null) {
    //    //    return RespBean.error(RespBeanEnum.LOGIN_ERROR);
    //    //}
    //    User user = userMapper.selectById(mobile);
    //    if (!MD5Util.passChange2(password, user.getSalt()).equals(user.getPassword())) {
    //        return RespBean.error(RespBeanEnum.LOGIN_ERROR);
    //    }
    //    //生成cookie
    //    String ticket = UUIDUtil.uuid();
    //    //实现session方法1
    //    //request.getSession().setAttribute(ticket,user);
    //    //方法2用户信息存入redis
    //    redisUtil.set("user:" + ticket, user);
    //    CookieUtil.setCookie(request, response, "userTicket", ticket);
    //    return RespBean.success(ticket);
    //}

    @Override
    public RespBean login(HttpServletRequest request, HttpServletResponse
            response,LoginVo loginVo) {
        String mobile = loginVo.getMobile();
        String password = loginVo.getPassword();
        //根据手机号获取用户
        User user = userMapper.selectById(mobile);
        if (null == user) {
            throw new GlobalException(RespBeanEnum.LOGIN_ERROR);
        }
        //校验密码
        if (!MD5Util.passChange2(password, user.getSalt()).equals(user.getPassword())) {
            return RespBean.error(RespBeanEnum.LOGIN_ERROR);
        }
        //生成cookie
        String ticket = UUIDUtil.uuid();
        //request.getSession().setAttribute(ticket, user);
        //在redis中保存
        redisUtil.set("user:" + ticket, user);
        //设置
        CookieUtil.setCookie(request, response, "userTicket", ticket);
        //这里带上ticket主要是为了生成5000个用户测试时具有cookie
        return RespBean.success(ticket);
    }

    @Override
    public User getUserByCookie(String userTicket, HttpServletRequest request, HttpServletResponse response) {
        User user = (User) redisUtil.get("user:" + userTicket);
        if (user == null) {
            CookieUtil.setCookie(request, response, "userTicket", userTicket);
            redisUtil.set("user:" + userTicket, user);
        }
        return user;
    }

    @Override
    public RespBean updatePassword(String userTicket, String password, HttpServletRequest request, HttpServletResponse response) {
        User user = getUserByCookie(userTicket, request, response);
        if (user == null) {
            System.out.println("cookie中的user为null");
        }
        user.setPassword(MD5Util.realPass(password, user.getSalt()));
        if (1 == userMapper.updateById(user)) {
            //删除redis中的缓存 因为密码更新了
            redisUtil.del("user:" + userTicket);
            return RespBean.success();
        }
        return RespBean.error(RespBeanEnum.UPDATE_ERROR);
    }
}
